package com.example.login;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;



public class LoginActivity extends AppCompatActivity {
    private Button login;//登录按钮
    private Button register;//注册按钮
    private EditText ed1,ed2;//编辑框
    private CheckBox checkBox;//记住密码
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        checkBox = (CheckBox) findViewById(R.id.checkBox);
        Button buttonLogin=(Button) findViewById(R.id.login);
        final EditText editTextUerName=findViewById(R.id.ed1);
        final EditText editTextPassword=findViewById(R.id.ed2);
        boolean isRemember = pref.getBoolean("checkBox",false);
        if (isRemember){
            String UserName = pref.getString("UserName","");
            String Password = pref.getString("Password","");
            editTextUerName.setText(UserName);
            editTextPassword.setText(Password);
            checkBox.setChecked(true);
            String userName = editTextUerName.getText().toString();
            String password = editTextPassword.getText().toString();
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            intent.putExtra("userName", userName);
            intent.putExtra("password", password);
            startActivity(intent);
        }
        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String userName = editTextUerName.getText().toString();
                boolean name = userName.equals("admin");
                String password = editTextPassword.getText().toString();
                boolean pass = password.equals("admin");
                if (TextUtils.isEmpty(editTextUerName.getText().toString()) || TextUtils.isEmpty(editTextPassword.getText().toString())) {
                    Toast.makeText(LoginActivity.this, "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();
                } else if (name && pass) {
                    editor = pref.edit();
                    if (checkBox.isChecked()) {
                        editor.putBoolean("checkBox", true);
                        editor.putString("UserName", userName);
                        editor.putString("Password", password);
                    }else{
                        editor.clear();
                    }
                    editor.apply();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    intent.putExtra("userName", userName);
                    intent.putExtra("password", password);
                    startActivity(intent);
                }
                else{
                    AlertDialog.Builder dialog =new AlertDialog.Builder(LoginActivity.this);
                    dialog.setTitle("用户名或者密码错误");
                    dialog.setMessage("");
                    dialog.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int which) {
                        }
                    });
                    dialog.show();
                }
            }
        });

    }

}